/*
 * To change this template, choose Tools | Templates and open the template in
 * the editor.
 */
package SteamEngineFX.Layouts;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import javafx.beans.InvalidationListener;
import javafx.beans.Observable;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.control.ContentDisplay;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import SteamEngineFX.Config.Config;
import SteamEngineFX.Controls.Calendar.schudt.DateHyperlink;
import SteamEngineFX.SteamEngineFX;
import javafx.scene.Cursor;
import org.joda.time.LocalDate;

/**
 *
 * @author jbendixsen
 */
//public class TopPane extends HBox{
public class TopPane extends AnchorPane{
    
    private Stage primaryStage = null;
    private ApplicationPane appPane = null;

    //private static final Image Calendar24 = new Image(SteamEngineFX.class.getResourceAsStream("/steamenginefx/resources/calendar_24.png"));
    private static final Image Gears24 = new Image(SteamEngineFX.class.getResourceAsStream("/steamenginefx/resources/gears_24.png"));
    
    //private Hyperlink btnSelectDate = null;
    private Hyperlink btnShowConfig = null;
    private DateHyperlink dateHyperlink = null;
    
    private DateFormat dateFormat = new SimpleDateFormat("EEEE MMMM d, yyyy");    
    
    public TopPane(ApplicationPane AppPane, Stage PrimaryStage) {
        
        primaryStage = PrimaryStage;
        appPane = AppPane;
                
        setPrefHeight(90);
        setPadding(new Insets(5, 5, 5, 5));
        getStyleClass().add("TopPane");
        
        HBox appTitleBox = new HBox();
        appTitleBox.setAlignment(Pos.CENTER_LEFT);
        appTitleBox.setSpacing(10);
        appTitleBox.setPrefWidth(400);
        
        Label appTitleStart = new Label("Kettle Valley Steam Railway");
        appTitleStart.getStyleClass().add("appTitleStart");
                
//        Label appTitleEnd = new Label("Reservations");
//        appTitleEnd.getStyleClass().add("appTitleEnd");
        
        appTitleStart.setEffect(new DropShadow());
//        appTitleEnd.setEffect(new DropShadow());
        
//        appTitleBox.getChildren().addAll(appTitleStart, appTitleEnd);
        appTitleBox.getChildren().addAll(appTitleStart);
        
        ImageView ivGears = new ImageView(Gears24);
        
        btnShowConfig = new Hyperlink("Configuration");
        btnShowConfig.setGraphic(ivGears);
        btnShowConfig.setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
        btnShowConfig.getStyleClass().add("btnShowConfig");
        btnShowConfig.setOnAction(new EventHandler<ActionEvent>(){
            @Override
            public void handle(ActionEvent e){
                ShowConfig();
            }
        });
        
        dateHyperlink = new DateHyperlink();
        dateHyperlink.setDateFormat(dateFormat);
        dateHyperlink.selectedDateProperty().addListener(new InvalidationListener() {
            @Override
            public void invalidated(Observable observable){
                //appPane.setSelectedDate(dateHyperlink.selectedDateProperty().get());
                setCursor(Cursor.WAIT);
                try{
                    appPane.setSelectedDate(new LocalDate(dateHyperlink.selectedDateProperty().get()));
                }finally{
                    setCursor(Cursor.DEFAULT);
                }
            }
        });
        
        getChildren().addAll(appTitleBox, dateHyperlink, btnShowConfig);
        
        setLeftAnchor(appTitleBox, Double.valueOf(0));
        setTopAnchor(appTitleBox, Double.valueOf(0));
        
        setLeftAnchor(dateHyperlink, Double.valueOf(0));
        setTopAnchor(dateHyperlink, Double.valueOf(40));
        
        setRightAnchor(btnShowConfig, Double.valueOf(0));
        setTopAnchor(btnShowConfig, Double.valueOf(0));
    }
    
    public void initSelectedDate(LocalDate NewDate){
        dateHyperlink.setSelectedDate(NewDate.toDate());
    }
    
    public void ShowConfig(){
        Config config = new Config();
        config.ShowConfig();
    }
    
    private void setStatus(String NewStatus){
        appPane.setStatus(NewStatus);
    }
        
    private void setDebug(String DebugText){
        appPane.setDebug(DebugText);
    }
        
}
